package com.example.final_project.service.impl;

import com.example.final_project.entity.User;
import com.example.final_project.mapper.UserMapper;
import com.example.final_project.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getList(String realName, String department, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectList(realName, department);
        return new PageInfo<>(list);
    }

    @Override
    public boolean delete(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public boolean add(User user) {
        // 查找最小可用ID
        List<Long> existingIds = userMapper.selectAllIds();
        Long newId = 1L;
        if (existingIds != null && !existingIds.isEmpty()) {
            for (Long id : existingIds) {
                if (id.equals(newId)) {
                    newId++;
                } else {
                    break; // 找到空缺
                }
            }
        }
        user.setId(newId);
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean checkUsernameExist(String username) {
        return userMapper.countByUsername(username) > 0;
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public boolean update(User user) {
        // 检查用户名是否已存在（排除当前用户）
        User existingUser = userMapper.selectById(user.getId());
        if (existingUser != null && !existingUser.getUsername().equals(user.getUsername())) {
            if (userMapper.countByUsername(user.getUsername()) > 0) {
                return false;
            }
        }
        return userMapper.update(user) > 0;
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.selectByUsername(username);
        if (user != null && password.equals(user.getPassword())) {
            return user;
        }
        return null;
    }

    @Override
    public int countAllUsers() {
        return userMapper.countAllUsers();
    }

    @Override
    public Map<String, Integer> getUserTypeStats() {
        List<Map<String, Object>> list = userMapper.getUserTypeStats();
        Map<String, Integer> result = new HashMap<>();
        for (Map<String, Object> map : list) {
            String type = (String) map.get("type");
            Integer cnt = ((Number) map.get("cnt")).intValue();
            result.put(type, cnt);
        }
        return result;
    }

    @Override
    public List<User> getLatestUsers(int limit) {
        return userMapper.getLatestUsers(limit);
    }
} 